;var PRO=PRO||{};
PRO.Jutils=(function(){
	var M={

	};
	function  extendsJQ() {
		$.extend({
			//显示loading
			showLoading: function () {
				if (!$("#_loading").length) $("<div id=\"_loading\" class=\"loading\"><div></div><div></div><div></div>数据加载中，请稍后...</div>").appendTo("body");
			},
			//移除loading
			removeLoading: function () {
				if ($("#_loading").length) $("#_loading").remove();
			},
			//启动按钮
			enable: function (el) {
				if (typeof (el) == "undefined" || el === null || el === "") return;
				$(el).removeClass("btn-disabled");
				$.removeLoading();
			},
			//禁用按钮
			disable: function (el) {
				if (typeof (el) == "undefined" || el === null || el === "") return;
				$(el).addClass("btn-disabled");
				$.showLoading();
			},
			//是否被禁用
			isDisabled: function (el) {
				return $(el).hasClass("btn-disabled");
			},
			//设置缓存数据
			setCache: function (key, value) {
				if (!key) return false;
				window.localStorage ? window.localStorage.setItem(key, value) : $.cookie(key, value);
			},
			//获取缓存数据
			getCache: function (key) {
				if (!key) return null;
				return window.localStorage ? window.localStorage.getItem(key) : $.cookie(key);
			},
			//回到顶部
			gotop: function () {
				if ($("#goTop").length) $("#goTop").remove();
				$("<a class=\"go-top\" id=\"goTop\" href=\"javascript:;\"><i></i></a>").appendTo("body").off("click").on("click", function () {
					window.scrollTo(0, 0);
				});

				var top = document.documentElement.scrollTop || document.body.scrollTop;
				top > 500 && $("#goTop").show();

				$("#goTop").hover(function () {
					$(this).html("顶部");
				}, function () {
					$(this).html("<i></i>");
				});

				$(window).scroll(function () {
					var _top = document.documentElement.scrollTop || document.body.scrollTop;
					_top <= 50 ? $("#goTop").hide() : $("#goTop").show();
				});
			},
			//动态加载css文件
			getCss: function (file, version, callback) {
				var files = typeof file == "string" ? [file] : file;
				for (var i = 0; i < files.length; i++) {
					var name = files[i].replace(/^\s|\s$/g, "");
					var att = name.split(".");
					var ext = att[att.length - 1].toLowerCase();
					var isCSS = ext == "css";
					var tag = isCSS ? "link" : "script";
					var attr = isCSS ? " type=\"text/css\" rel=\"stylesheet\" " : " type=\"text/javascript\" ";
					var link = (isCSS ? "href" : "src") + "=\"" + name + "?" + (version || new Date().getTime()) + "\"";

					if (!$(tag + "[" + link + "]").length) {
						isCSS ? $("head").append("<" + tag + attr + link + " />") : $("head").append("<" + tag + attr + link + "></" + tag + ">");
					}
				}
				callback && callback();
			},
			//格式化money, s:money, n:要显示的小数位数(不传此参数默认显示两位，自动四舍五入)
			formatMoney: function (s, n) {
				n = n > 0 && n <= 20 ? n : 2;
				s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
				var l = s.split(".")[0].split("").reverse(),
					r = s.split(".")[1],
					t = "";
				for (i = 0; i < l.length; i++) {
					t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
				}
				return t.split("").reverse().join("") + "." + r;
			},
			scrollTo: function (selector, callback) {
				var _el = $(selector),
					_timer,
					_dead_time = 100,
					_min_dead_time = 10,
					_first_position = document.documentElement.scrollTop || document.body.scrollTop,
					_distance,
					_min_distance = 200,
					_step,
					_top_position,
					_curr_position,
					_scroll_height = $(document).height(),
					_window_height = $(window).height(),
					_runTop = function () {
						_curr_position = document.documentElement.scrollTop || document.body.scrollTop;
						_curr_position += _step;
						if ((_step < 0 && _curr_position >= _top_position) || (_step > 0 && _curr_position <= _top_position)) {
							if (_step > 0 && (_curr_position + _window_height >= _scroll_height)) {
								clearInterval(_timer);
								callback && callback();
							}
							window.scrollTo(0, _curr_position);
						}
						else {
							clearInterval(_timer);
							callback && callback();
						}
					};
				if (!_el || !_el.length) return false;
				_top_position = _el.offset().top;
				_distance = _top_position - _first_position;

				_step = _distance / (Math.abs(_distance) <= _min_distance ? _min_dead_time : _dead_time);
				_timer = setInterval(_runTop, 1);
			},
			//tips
			tips: tips
		});
		$.fn.form2Json = function(){
			var serializeObj={};
			var array=this.serializeArray();
			var str=this.serialize();
			$(array).each(function(){
				if(serializeObj[this.name]){
					if($.isArray(serializeObj[this.name])){
						serializeObj[this.name].push(this.value);
					}else{
						serializeObj[this.name]=[serializeObj[this.name],this.value];
					}
				}else{
					serializeObj[this.name]=this.value;
				}
			});
			return serializeObj;
		};
	}
	/**
	 * @function:
	 * tips提示信息
	 * @parameters:
	 * message:提示信息内容文体(必要参数)
	 * type:提示类型(int) 1:成功,0:失败(默认是1)
	 * timeout:延迟关闭时间,单位:毫秒(默认1000)
	 * callback:提示信息完成后执行的回调函数(可选)
	 */
	function tips(message, type, timeout, callback) {
		var _id = "_tips_box", _el, _marginTop, _marginLeft, _timer, _type;
		type == 0 ? _type = "error" : _type = "success";
		if ($("#" + _id).length) $("#" + _id).remove();
		$("<div id=\"" + _id + "\" class=\"tips tips-" + _type + "\"></div>").appendTo("body");
		_el = $("#" + _id);
		_el.html("<img src=\"/yop/static/app/yop/images/tips/" + _type + ".png\" />" + message + "<div class=\"tips-x tips-" + _type + "-x\"></div>");
		_marginLeft = _el.outerWidth() / 2;
		_marginTop = _el.outerHeight() / 2;
		_el.css({ marginLeft: -_marginLeft + "px", marginTop: -_marginTop + "px" });
		_el.animate({ opacity: "1" }, function () {
			_timer = setTimeout(function () {
				_el.animate({ opacity: "0" }, function () {
					_el.remove();
					callback && callback();
				});
			}, timeout || 1000);
		});
		_el.find(".tips-x").click(function () {
			_el.animate({ opacity: "0" }, function () {
				_el.remove();
			});
		});
	}
	function formatDate(){
		Date.prototype.format = function (fmt) { //author: meizz
			var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
			};
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
			for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			return fmt;
		}
	}
	return {
		//页面初始化
		init:function(){
			//初始化拓展jquery
			extendsJQ();
			formatDate();
		},M:M
	}
})();
$(function(){
	PRO.Jutils.init();
});